//
//  BaseViewController.m
//  CarLifes
//
//  Created by yu on 2017/6/15.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "BaseViewController.h"
#import "UIImage+CLImage.h"
#import "PPNetworkHelper.h"
#import <IQKeyboardReturnKeyHandler.h>

@interface BaseViewController ()
{
    IQKeyboardReturnKeyHandler *returnKeyHandler;
}

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置默认的(蓝色)导航条
    [self setDefaultNavigationBar];
    // 此处统一设置键盘return类型
    [self setupReturnKeyType];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)setupReturnKeyType
{
    returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
    returnKeyHandler.lastTextFieldReturnKeyType = UIReturnKeyDone;
}



- (void)setLeftImageNamed:(NSString *)name action:(SEL)action {
    _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn.frame = CGRectMake(0, 0, 33, 33);
    UIImage *image = [UIImage imageNamed:name];
    [_leftBtn setImage:image forState:UIControlStateNormal];
    [_leftBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    _leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 5);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:_leftBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    @weakify(self);
    [_leftBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        @strongify(self);
        if (!action) {
            [self popBackLastController];
        }
    }];
}

- (void)setLeftImageNamed:(NSString *)name title:(NSString *)title action:(SEL)action {
    _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn.frame = CGRectMake(0, 0, 30, 40);
    _leftBtn.backgroundColor = [UIColor clearColor];
    UIImage *image = [UIImage imageNamed:name];
    [_leftBtn setImage:image forState:UIControlStateNormal];
    _leftBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_leftBtn addTarget:self action:action forControlEvents:
     UIControlEventTouchUpInside];
    _leftBtn.titleLabel.font = SYSTEM_FONT(10);
    [_leftBtn setTitle:title forState:UIControlStateNormal];
    [_leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _leftBtn.imageEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, 0);
    _leftBtn.titleEdgeInsets = UIEdgeInsetsMake(25, -30, 0, 0);
//    _leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:_leftBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    @weakify(self);
    [_leftBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        @strongify(self);
        if (!action) {
            [self popBackLastController];
        }
    }];
}

- (void)popBackLastController {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setRightNormalImageNamed:(NSString *)normalImage selectImageNamed:(NSString *)selectImage action:(SEL)action {
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _rightBtn.backgroundColor = [UIColor orangeColor];
    _rightBtn.frame = CGRectMake(0, 0, 33, 33);
    [_rightBtn setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
    [_rightBtn setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
    _rightBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_rightBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -10;

    self.navigationItem.rightBarButtonItems = @[space,[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
}

- (void)setRightNormalImageNamed:(NSString *)normalImage selectImageNamed:(NSString *)selectImage title:(NSString *)title action:(SEL)action {
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn.frame = CGRectMake(0, 0, 30, 40);
    _rightBtn.backgroundColor = [UIColor clearColor];
    _rightBtn.imageEdgeInsets = UIEdgeInsetsMake(-10, 10, 0, 0);
    _rightBtn.titleEdgeInsets = UIEdgeInsetsMake(25, -10, 0, 0);
//    _rightBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [_rightBtn setTitle:title forState:UIControlStateNormal];
    [_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _rightBtn.titleLabel.font = SYSTEM_FONT(10);
    [_rightBtn setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
    [_rightBtn setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
     _rightBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_rightBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn];
}

- (void)setRightItemTitle:(NSString *)title action:(SEL)action {
    [self setRightItemTitle:title titleColor:[UIColor whiteColor] action:action];
}

- (void)setRightItemTitle:(NSString *)title titleColor:(UIColor *)color action:(SEL)action {
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    NSDictionary *attributes = @{NSFontAttributeName: SYSTEM_FONT(15)};
    CGSize size = [title sizeWithAttributes:attributes];
    //! 这里需要根据内容大小来调整宽度
    _rightBtn.frame = CGRectMake(0, 0, size.width <= 10 ? 70 : size.width + 10, 44);
    [_rightBtn setTitleColor:color forState:UIControlStateNormal];
    [_rightBtn setTitleColor:[color colorWithAlphaComponent:0.5] forState:UIControlStateHighlighted];
    [_rightBtn setTitle:title forState:UIControlStateNormal];
    _rightBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    _rightBtn.titleLabel.font = SYSTEM_FONT(15);
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -5;
    
    [_rightBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn];
    self.navigationItem.rightBarButtonItems = @[negativeSpacer, backItem];
    
}

- (void)setDefaultNavigationBar {
    if (@available(iOS 13.0, *)) {
        UIColor *navColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return kBaseColor;
            } else {
                return UIColor.tertiarySystemBackgroundColor;
            }
        }];
        
        [self setNavigationBarBackgroundImage:nil
             tintColor:navColor
             textColor:[UIColor whiteColor]
        statusBarStyle:UIStatusBarStyleLightContent];
        
    } else {
        [self setNavigationBarBackgroundImage:nil
             tintColor:kBaseColor
             textColor:[UIColor whiteColor]
        statusBarStyle:UIStatusBarStyleLightContent];
    }
    
//    [self setNavigationBarBackgroundImage:nil
//                                tintColor:kBaseColor
//                                textColor:[UIColor whiteColor]
//                           statusBarStyle:UIStatusBarStyleLightContent];
}

- (void)setWhiteNavigationBar {
    if (@available(iOS 13.0, *)) {
        UIColor *navColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return UIColor.whiteColor;
            } else {
                return UIColor.tertiarySystemBackgroundColor;
            }
        }];
        
        UIColor *titleColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return kTextBlackColor;
            } else {
                return kDarkTextColor;
            }
        }];
        
        [self setNavigationBarBackgroundImage:nil
             tintColor:navColor
             textColor:titleColor
        statusBarStyle:UIStatusBarStyleDarkContent];
        
    } else {
        [self setNavigationBarBackgroundImage:nil
             tintColor:[UIColor whiteColor]
             textColor:kTextBlackColor
        statusBarStyle:UIStatusBarStyleDefault];
    }
    
    
//    [self setNavigationBarBackgroundImage:nil
//                                tintColor:[UIColor whiteColor]
//                                textColor:kTextBlackColor
//                           statusBarStyle:UIStatusBarStyleDefault];
}


- (void)setClearNavigationBar {
    [self setNavigationBarBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]]
                                tintColor:[UIColor clearColor]
                                textColor:[UIColor clearColor]
                           statusBarStyle:UIStatusBarStyleDefault];
}


- (void)setNavigationBarBackgroundImage:(UIImage *)image
                              tintColor:(UIColor *)tintColor
                              textColor:(UIColor *)textColor
                         statusBarStyle:(UIStatusBarStyle)style {
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBarTintColor:tintColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:textColor,NSFontAttributeName:SYSTEM_FONT(18)};
    [UIApplication sharedApplication].statusBarStyle = style;
}

- (RequestTool *)requestTool {
    if (_requestTool == nil) {
        _requestTool = [[RequestTool alloc] init];
    }
    return _requestTool;
}

- (User *)user {
    if (_user == nil) {
        _user = [User sharedUser];
    }
    return _user;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
